package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.sellerquery2.parameter.GetRefundInfoBean;
import com.ymatou.iapi.trading.sellerquery2.parameter.resp.GetRefundInfoResp;
import com.ymatou.iapi.trading.sellerquery2.service.GetRefundInfoCall;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.common.entitie.RefundBill;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 获取退款信息接口 FreeMaker Template自动生成代码
 * 接口已经转接java
 */
@SuppressWarnings("rawtypes")
@TestSuite
public class Ts_GetRefundInfo {
	private static GetRefundInfoBean getrefundinfoBean;
	private static GetRefundInfoCall getrefundinfoCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取退款信息接口");
	}

	@Before
	public void caseUp() {
		getrefundinfoBean = new GetRefundInfoBean();
		getrefundinfoCall = new GetRefundInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getrefundinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void verify(){
		GetRefundInfoResp expect = new GetRefundInfoResp();
		ConstructExpectedResponse(expect);
		GetRefundInfoResp actual = getrefundinfoCall.getRefundInfoResp();
		DataUtil.verify(expect.getOrderId(), actual.getOrderId(),"验证orderId");
		DataUtil.verify(expect.getRefundNo(), actual.getRefundNo(),"验证refundNo");
		DataUtil.verify(expect.getTotal(), actual.getTotal(),"验证total");
		DataUtil.verify(expect.getRefundedCash(), actual.getRefundedCash(),"验证refundedCash");
		DataUtil.verify(expect.getRefundedGift(), actual.getRefundedGift(),"验证refundedGift");
	}
	
	private void ConstructExpectedResponse(GetRefundInfoResp response){
		Map m = execute(DataUtil.Stringg.getValueOrDefault(getrefundinfoBean.getRefundNo()));
		if(m != null){
			RefundBill rb = new RefundBill();
			DataUtil.mapToBean(rb, m);
			response.setOrderId(rb.getOrderId());
			response.setRefundedCash(rb.getRefundedAmountOfCash());
			response.setRefundedGift(rb.getRefundedAmountOfGift());
			response.setRefundNo(rb.getRefundBillNo());
			response.setTotal(rb.getRefundAmount());
		}
	}
	
	private Map execute(String refundBillNo){
		List<Map> list = new DefaultWapper().executeSql(getOrderRefundInfoSql(refundBillNo));
		if(DataUtil.isNullOrEmpty(list)){
			return null;
		}
		return list.get(0);
	}

	public static String getOrderRefundInfoSql(String refundBillNo) {
		return "SELECT TOP 1 OrderId,RefundBillNo,RefundAmount,RefundedAmountOfCash,RefundedAmountOfGift"
				+ " FROM ymt_refundbill  (NOLOCK)"
				+ " WHERE RefundBillNo = '"+refundBillNo+"'";
	}

	private String getRefundNo(int orderId){
		return (String)new DefaultWapper().executeSql("select top 1 RefundBillNo from ymt_refundbill where orderid = "+orderId).get(0).get("RefundBillNo");
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetRefundInfo_001() {
		Logger.start(true, "获取退款信息成功");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create4_RefundOrder(userId, sellerId).getiOrderId();
			Thread.sleep(500l);
			Logger.comment("准备数据");
			getrefundinfoBean.setRefundNo(getRefundNo(orderId));
			String param=FormatUtil.beanToHttpString(getrefundinfoBean, true, false);
			getrefundinfoCall.setData(param);
			getrefundinfoCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getrefundinfoCall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getrefundinfoCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetRefundInfo_002() {
		Logger.start(true, "退款单号不存在");
		try {
			getrefundinfoBean.setRefundNo("404");
			String param=FormatUtil.beanToHttpString(getrefundinfoBean, true, false);
			getrefundinfoCall.setData(param);
			getrefundinfoCall.callService();

			DataUtil.verifyEquals(ECode.SUCCESS, getrefundinfoCall.getCode(),
					"验证code");
			DataUtil.verifyEquals(0, getrefundinfoCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetRefundInfo_003() {
		Logger.start(true, "退款单号为null");
		try {
			String param=FormatUtil.beanToHttpString(getrefundinfoBean, true, false);
			getrefundinfoCall.setData(param);
			getrefundinfoCall.callService();
			getrefundinfoCall.callService();

			DataUtil.verifyEquals(ECode.SUCCESS, getrefundinfoCall.getCode(),
					"验证code");
			DataUtil.verifyEquals(0, getrefundinfoCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}